<template>
  <div class="icons">
    <swiper :options="swiperOption" ref="mySwiper" class="icons-swiper">
      <swiper-slide v-for="(page,index) in pages" :key="index" class="swiper-item clearfix">
        <div class="icons-item" v-for="item in page" :key="item.id">
          <a href="#">
            <img :src="item.imgUrl" alt />
            <p>{{item.desc}}</p>
          </a>
        </div>
      </swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
  </div>
</template>

<script>
import { swiper, swiperSlide } from "vue-awesome-swiper";
export default {
  name: "HomeIcons",
  data() {
    return {
      swiperOption: {
        pagination: {
          el: ".swiper-pagination"
        }
      },
      msg: null
    };
  },
  computed: {
    pages: function() {
      /**
       * pages为一个二维数组，里面的每一个数组，即代表每一页
       */
      var pages = [];
      var page = 0;
      this.iconList.forEach((item, index) => {
        page = Math.floor(index / 8);
        // page即为pages的下标，页码。
        if (!pages[page]) {
          // 对应下标的数组不存在时，先创建一个数组
          pages[page] = [];
        }
        pages[page].push(item);
      });
      return pages;
    }
  },
  props: {
    iconList: Array
  }
};
</script>

<style lang="stylus" scoped>
@import '~@/assets/style/mixins'

.icons >>> .swiper-container
  height: 3.9rem
.icons
  padding-top: 0.2rem
  width: 100%
  height: 4rem
  // display: flex
  // flex-wrap: wrap
  // 使用swiper时，不能使用flex,会造成样式错乱
  .icons-item
    float: left
    width: 25%
    height: 1.6rem
    a
      display: block
      width: 100%
      color: black
      font-size: 0.28rem
      img
        display: block
        margin: 0 auto
        width: 1.1rem
      p
        text-align: center
        updateText()
</style>